class EndLessItemUI extends eui.ItemRenderer{
    private oldData:any;
    private img_icon:eui.Image;
    private img_selected:eui.Image;
    private img_bg_1:eui.Image;
    private img_bg_2:eui.Image;

    constructor()
    {
        super();
        this.skinName = 'EndLessItemView';
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.select, this);
    }

    public select()
    {
        if(this.img_selected.visible)return;
        let data = this.data;
        let weaponId = data.id;
        GameUtil.dispatchEvent(EventConst.ENDLESS_CHANGE_WEAPON,{index:this.itemIndex, weaponId:weaponId});
    }

    private onRemoveToStageHandler(e:egret.Event){
		this.clear();
	}

    public init()
    {
        let data = this.data;
        if(data)
        {
            let weaponId = data.id;
            this.img_selected.visible = false;
            // this.img_icon.width = this.img_icon.height = 102;
            this.img_icon.source = 'knife_'+weaponId;
            this.img_icon.width = this.img_icon.texture.textureWidth;
            this.img_icon.height = this.img_icon.texture.textureHeight;
            if(this.img_icon.height > 160)
            {
                this.img_icon.scaleX = this.img_icon.scaleY = 0.4;
            }
            this.name = 'knife_'+weaponId;
            let logic:EndLessUI = UIManager.instance().getLogicByName('EndLessUI') as EndLessUI;
            if((data.index == 1 && !logic.selectWeaponItem.curSelectItem))
            {
                GameUtil.dispatchEvent(EventConst.ENDLESS_CHANGE_WEAPON,{index:this.itemIndex, weaponId:weaponId});
            }
        }
    }

    public setSelectState()
    {
        this.img_selected.visible = true;
        this.img_bg_1.visible = false;
        this.img_bg_2.visible = true;
    }

    public cancelSelectState()
    {
        this.img_selected.visible = false;
        this.img_bg_1.visible = true;
        this.img_bg_2.visible = false;
    }

    public dataChanged()
    {
        if(this.data == this.oldData)return;
        this.oldData = this.data;
        this.init();
    }

    public clear()
    {
        this.data = null;
        this.oldData = null;
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler,this);
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.select, this);
    }
}

window['EndLessItemUI'] = EndLessItemUI;